var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contacts: [],
    winHeight: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    this.getData(that);
  },
  getData: function (that) {

    util.request(api.ContactsList, { token: wx.getStorageSync("token") }).then(function (res) {
      if (res.code == '200') {
        that.setData({
          contacts: res.datas
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  fnToNew: function () {
    wx.navigateTo({
      url: '/pages/perCenter/contactsNew/index',
    })
  },

  fnEdit: function (e) {
    var id = e.currentTarget.dataset.id
    var name = e.currentTarget.dataset.name
    var telephone = e.currentTarget.dataset.telephone
    var idCard = e.currentTarget.dataset.idcard
    var isdefault = e.currentTarget.dataset.isdefault
    wx.navigateTo({
      url: '/pages/perCenter/contactsNew/index?id=' + id + "&name=" + name + "&telephone=" + telephone + "&idCard=" + idCard +"&isdefault=" + isdefault+"&type=edit"
    })
  },
  fnDel: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var obj = new Object();
    obj.id = id;
    wx.showLoading({
      title: '请稍候',
    })
    util.request(api.ContactsOper, { token: wx.getStorageSync("token"), contacts: obj, type: "del" }).then(function (res) {
      wx.hideLoading()
      if (res.code == '200') {
        that.getData(that)
      }
    });
  },
  updateDefault: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    var isdefault = e.currentTarget.dataset.isdefault
    if (isdefault == "true") {
      wx.showToast({
        title: '不能取消默认',
      })
      return;
    }
    var obj = new Object();
    obj.id = id;
    obj.isDefault = true;
    wx.showLoading({
      title: '请稍候',
    })
    util.request(api.ContactsOper, { token: wx.getStorageSync("token"), contacts: obj, type: "edit" }).then(function (res) {
      wx.hideLoading()
      if (res.code == '200') {
        that.getData(that)
      }
    });
  }
})